package com.zfl.thread.day03;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class Demo03 {
    public static Object lock = new Object();

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Light implements Runnable {
        public static volatile int flag = 1;
        private String name;
        private int state;


        @Override
        public void run() {
            synchronized (lock) {
                while (this.state != flag) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                for (int i = 0; i < 5; i++) {

                    System.out.println(this.name + "-" + i + "-");

                }
                flag = (this.state + 1) > 3 ? 1 : this.state + 1;
                lock.notifyAll();

            }
        }
    }

    public static void main(String[] args) {

        new Thread(new Light("红灯", 1)).start();
        new Thread(new Light("绿灯", 2)).start();
        new Thread(new Light("黄灯", 3)).start();


    }


}
